<template>
  <div id="blue_v">
    <p class="data_time">
      <span>数据更新时间：</span>
      <span style="color:#888">{{ timestamp(time, "Y-M-D h:m") }}</span>
    </p>
    <category_industry v-model="industryItemObj" :class="'mt_30'"/>
    <section class="filter">
      <p>筛选</p>
      <el_select
        v-model="value04"
        title="粉丝总数"
        :options="[
          '不限',
          '10万-50万',
          '50万-100万',
          '100万-500万',
          '500万-1000万',
          '>1000万'
        ]"
      />
     <multi_select v-model="value05" :options="['是否带货']" />
    </section>
    <section class="type">
      <time_options v-model="value06" :radio_values="['日榜', '周榜', '月榜']" :timing="$global.timing"/>
      <section class="input_group">
        <input
          type="text"
          v-model="input01"
          placeholder="请输入关键词在排行榜中搜索"
          @keyup.enter="search_input()"
        />
        <div @click="search_input()" class="imgBox">
        </div>
      </section>
      <div class="btn_group">
        <button @click="generatorImage">
          <span></span>分享
        </button>
        <button @click="export_data">
          <span></span>导出
        </button>
      </div>
    </section>
    <ul
      class="fixedThead"
      ref="fixedThead"
      :class="{ isShow: isFixed }"
      :style="showLeft == true ? 'left:222px' : 'left:98px'"
    >
      <li tc>排名</li>
      <li>达人</li>
      <li
        tc
        v-if="
          sort == 0 &&
            this.value06.split('?')[1] != '2021-04-01-2021-04-30' &&
            this.value06.split('?')[1] != '2021-03-01-2021-03-31'
        "
      >
        粉丝总数
        <img src="@/assets/meng/down_up.png" class="sort" />
      </li>
      <li
        tc
        style=" cursor: pointer;"
        v-if="
          sort != 0 &&
            this.value06.split('?')[1] != '2021-04-01-2021-04-30' &&
            this.value06.split('?')[1] != '2021-03-01-2021-03-31'
        "
        @click="sort_fun(true, 1)"
      >
        粉丝总数
        <img src="@/assets/meng/no_down_up.png" class="sort" />
      </li>
      <!-- 预留 -->
      <li
        tc
        v-if="
          this.value06.split('?')[1] == '2021-04-01-2021-04-30' ||
            this.value06.split('?')[1] == '2021-03-01-2021-03-31'
        "
      >粉丝总数</li>

      <li
        tc
        v-if="
          sort == 1 &&
            this.value06.split('?')[1] != '2021-04-01-2021-04-30' &&
            this.value06.split('?')[1] != '2021-03-01-2021-03-31'
        "
      >
        点赞增量
        <img src="@/assets/meng/down_up.png" class="sort" />
      </li>
      <li
        tc
        v-if="
          sort != 1 &&
            this.value06.split('?')[1] != '2021-04-01-2021-04-30' &&
            this.value06.split('?')[1] != '2021-03-01-2021-03-31'
        "
        style=" cursor: pointer;"
        @click="sort_fun(false, 2)"
      >
        点赞增量
        <img src="@/assets/meng/no_down_up.png" class="sort" />
      </li>
      <!-- 预留 -->
      <li
        tc
        v-if="
          this.value06.split('?')[1] == '2021-04-01-2021-04-30' ||
            this.value06.split('?')[1] == '2021-03-01-2021-03-31'
        "
      >点赞增量</li>
      <li tc>评论增量</li>
      <li tc>转发增量</li>
      <li tc>操作</li>
    </ul>
    <table class="table" ref="capture">
      <thead>
        <tr>
          <th>排名</th>
          <th>达人</th>
          <th
            v-if="
              sort == 0 &&
                this.value06.split('?')[1] != '2021-04-01-2021-04-30' &&
                this.value06.split('?')[1] != '2021-03-01-2021-03-31'
            "
          >
            粉丝总数
            <img src="@/assets/meng/down_up.png" class="sort" />
          </th>
          <th
            style=" cursor: pointer;"
            v-if="
              sort != 0 &&
                this.value06.split('?')[1] != '2021-04-01-2021-04-30' &&
                this.value06.split('?')[1] != '2021-03-01-2021-03-31'
            "
            @click="sort_fun(true, 1)"
          >
            粉丝总数
            <img src="@/assets/meng/no_down_up.png" class="sort" />
          </th>
          <!-- 预留 -->
          <th
            v-if="
              this.value06.split('?')[1] == '2021-04-01-2021-04-30' ||
                this.value06.split('?')[1] == '2021-03-01-2021-03-31'
            "
          >粉丝总数</th>

          <th
            v-if="
              sort == 1 &&
                this.value06.split('?')[1] != '2021-04-01-2021-04-30' &&
                this.value06.split('?')[1] != '2021-03-01-2021-03-31'
            "
          >
            点赞增量
            <img src="@/assets/meng/down_up.png" class="sort" />
          </th>
          <th
            v-if="
              sort != 1 &&
                this.value06.split('?')[1] != '2021-04-01-2021-04-30' &&
                this.value06.split('?')[1] != '2021-03-01-2021-03-31'
            "
            style=" cursor: pointer;"
            @click="sort_fun(false, 2)"
          >
            点赞增量
            <img src="@/assets/meng/no_down_up.png" class="sort" />
          </th>
          <!-- 预留 -->
          <th
            v-if="
              this.value06.split('?')[1] == '2021-04-01-2021-04-30' ||
                this.value06.split('?')[1] == '2021-03-01-2021-03-31'
            "
          >点赞增量</th>
          <th>评论增量</th>
          <th>转发增量</th>
          <th>操作</th>
        </tr>
      </thead>
      <tbody>
        <tr v-for="(item, index) in table_data01" :key="'table' + index" :id="!index ? 'tr1' : ''">
          <td class="td td_01">
            <div>
              <span>{{ item.rank }}</span>
              <section>
                <span class="new_font" v-if="item.change == 999999">新进榜</span>
                <img
                  v-if="item.change != 999999"
                  :class="{ gray_img: item.change == 0 }"
                  :src="
                    '../../../../static/meng/fans0' +
                      (item.change > 0 ? 2 : item.change < 0 ? 3 : 1) +
                      '.png'
                  "
                />
                <span
                  :class="{
                    gray: item.change == 0,
                    red: item.change > 0,
                    blue: item.change < 0
                  }"
                  v-if="item.change != 999999"
                >{{ Math.abs(item.change) }}</span>
              </section>
            </div>
          </td>
          <td class="td td_02">
            <div>
              <img :src="item.logo" class="pointer" v-errorImg:talent @click="go_to_page01(item.authorId)" />
              <section>
                <div>
                  <img
                    v-if="item.isShop == 1"
                    class="img01"
                    src="../../../assets/meng/search_result01.png"
                  />
                  <span class="dot pointer" @click="go_to_page01(item.authorId)">{{ item.nickName }}</span>

                  <img v-if="item.verifyType == 1" src="../../../assets/meng/search_result02.png" />
                  <img v-if="item.verifyType == 2" src="../../../assets/meng/search_result03.png" />
                </div>
                <div class="dot">抖音号：{{ item.uniqueId }}</div>
              </section>
            </div>
          </td>
          <!-- <td class="td td_03">
            <div>{{ format_num(item.hint) }}</div>
          </td>-->
          <td class="td td_04">
            <div>{{ format_num(item.totalFans) }}</div>
          </td>
          <td class="td td_05">
            <div>{{ format_num(item.favoriteIncr) }}</div>
          </td>
          <td class="td td_06">
            <div>{{ format_num(item.commentIncr) }}</div>
          </td>
          <td class="td td_07">
            <div>{{ format_num(item.shareIncr) }}</div>
          </td>
          <td class="td td_08">
            <button @click="go_to_page01(item.authorId)">查看详情</button>
          </td>
        </tr>
      </tbody>
    </table>
    <div class="upgradeBox" v-if="!permissions">
      <RankUpgrade class="rankUpgrade" />
    </div>
    <!-- <p
      class="nothing"
      v-show="
        table_data01.length < 30 &&
          !loading &&
          permissions &&
          table_data01.length != 0
      "
    >我是有底线的～</p> -->
    <div class="pageBox" v-if="!loading">
      <el-pagination
        background
        layout="prev, pager, next, jumper"
        :total="count"
        :page-size="30"
        :current-page="page"
        @current-change="get_data"
        :hide-on-single-page="true"
      ></el-pagination>
    </div>
    <div
      v-loading="loading"
      v-if="loading"
      class="loading"
      element-loading-text="拼命加载中..."
      element-loading-spinner="el-icon-loading"
    ></div>

    <div class="empty" v-show="!loading && table_data01.length == 0 && permissions">
      <img src="@/assets/liu/zw.png" />
      <p>暂无数据</p>
    </div>
  </div>
</template>

<script>
import html2canvas from 'html2canvas';
import RankUpgrade from '@/component/rankUpgrade.vue';
import category_industry from '@/component/category_industry.vue';
export default {
  name: 'blue_v',
  data() {
    let that = this;
    return {
      permissions: 1,
      busy: false,
      loading: true,
      table_data01: [],
      page: 1,
      count: 0,
      cancel01: null,
      input01: '',
      value04: '不限',
      value05: '',
      value06: '日榜',
      time_options: [],
      showLeft: true,
      isFixed: false,
      offsetTop: 0,
      sort: 0,
      orderType: 1,
      time: '',
      industryItemObj:{
        name:'全部',
        id:'0'
      }
    };
  },
  components: { RankUpgrade,category_industry },
  computed: {
    listen_change() {
      const { value04, value05, value06, industryItemObj } = this;
      return {
        value04,
        value05,
        value06,
        industryItemObj
      };
    }
  },
  watch: {
    // 搜索为空时自动刷新榜单
    input01: function (newVal, oldVal) {
      if (newVal == '') {
        this.table_data01 = [];
        this.page = 1;
        this.get_data();
      }
    },
    '$store.state.left_nav_flag'(old, newOld) {
      this.showLeft = newOld;
      if ((newOld = false)) {
        this.setTBBar();
      } else {
        this.setTBBar();
      }
    }
  },
  mounted() {
    window.addEventListener('scroll', this.initHeight);
    window.addEventListener('resize', () => {
      //给浏览器窗口绑定变化大小事件
      this.setTBBar();
    });
  },
  created: function () {
    // this.get_data() 因为time_options组件内部已经提交新值，导致v-model的值更改，所以已经执行请求函数
    this.$watch(
      'listen_change',
      this.debounce((newValue, oldValue) => {
        this.page = 1;
        this.table_data01 = [];
        this.get_data();
      }, 0)
    );
  },
  methods: {
    // 排序
    sort_fun(parm01, parm02) {
      if (!parm01) {
        this.sort = 1;
      } else {
        this.sort = 0;
      }
      this.orderType = parm02;
      this.page = 1;
      this.table_data01 = [];
      this.get_data();
    },
    // 获取子节点函数（后期可集成到一个库里）
    getChilds(pNode) {
      if (!pNode) return false;
      let nodes = pNode.childNodes;
      let newList = [];
      for (let i = 0; i < nodes.length; i++) {
        let node = nodes[i];
        if (node.nodeType === 1) {
          //判断类型
          newList.push(node);
        }
      }
      return newList;
    },
    // 设置表格表头滑动置顶
    setTBBar() {
      this.$nextTick(() => {
        let tableEl = this.$refs.tableEl;
        let ulEl = this.$refs.fixedThead;
        let tr1 = document.querySelector('#tr1');
        // thTrEl.style.width = tableEl.offsetWidth+'px';
        if (tr1) {
          let tds = this.getChilds(tr1);
          let liEls = this.getChilds(ulEl);

          for (let i = 0; i < liEls.length; i++) {
            liEls[i].style.width = tds[i].offsetWidth + 'px';
          }
        }
      });
    },
    search_input() {
      this.page = 1;
      this.table_data01 = [];
      this.get_data();
    },
    get_data(cpage) {
      let that = this;
      this.table_data01 = [];
      if (cpage) {
        that.page = cpage;
        that.backTop();
      }
      this.permissions = 1;
      this.loading = true;
      this.busy = true;

      this.$axios
        .post(
          '/api/author/FindBlueVRanks',
          {
            Category: this.industryItemObj.id,
            RankType:
              that.value06.split('?')[0] == '日榜'
                ? 1
                : that.value06.split('?')[0] == '周榜'
                ? 2
                : 3,
            FansNum: that.value04,
            IsShop: that.value05 != '' ? 1 : 0,
            Time: that.value06.split('?')[1],
            Keyword: this.trimStr(that.input01),
            Size: 30,
            Index: that.page,
            SortValue:
              that.value06.split('?')[1] == '2021-04-01-2021-04-30' ||
              that.value06.split('?')[1] == '2021-03-01-2021-03-31'
                ? 1
                : that.orderType
          },
          {
            cancelToken: new that.$axios.CancelToken(function executor(c) {
              if (that.cancel01) that.cancel01();
              that.cancel01 = c;
            })
          }
        )
        .then(response => {
          this.loading = false;
          if (response.data.code == 1013) {
            this.permissions = 0;
            this.table_data01 = [];
          } else {
            this.permissions = 1;
          }
          if (response.data.code == 0) {
            this.table_data01 = response.data.data.masters;
            this.time = response.data.data.timeSpan;
            response.data.data.masters.length < 50 ? (this.busy = true) : (this.busy = false);

            that.count = response.data.data.totalCount;
          }
          this.setTBBar();
        })
        .catch(error => {
          console.log(error);
        });
    },
    generatorImage() {
      let that = this;
      const loading = this.$loading({
        lock: true,
        text: '导出中...',
        spinner: 'el-icon-loading',
        customClass: 'customClass'
      });
      let shareContent = this.$refs.capture; //需要截图的包裹的（原生的）DOM 对象
      let canvas = document.createElement('canvas'); //创建一个canvas节点
      let scale = 1; //当前节点的1倍像素
      // let scale = 1.5 //当前节点的1.5倍像素
      // let scale = 2 //当前节点的2倍像素
      // let scale = window.devicePixelRatio //视网膜屏幕，设备像素比，好的显示器是2，普通显示器的是1。
      canvas.width = shareContent.offsetWidth * scale; //获取dom 宽度* 定义canvas 宽度 * 缩放
      canvas.height = shareContent.offsetHeight * scale; //获取dom 高度*定义canvas高度 *缩放
      canvas.getContext('2d').scale(scale, scale); //获取context,设置scale
      html2canvas(shareContent, {
        dpi: window.devicePixelRatio,
        scale: scale, // 添加的scale 参数
        useCORS: true,
        canvas: canvas, //自定义 canvas
        logging: true //日志开关
      }).then(canvas => {
        canvas.toBlob(
          function (blob) {
            const eleLink = document.createElement('a');
            eleLink.download = `《蓝V认证榜-${that.value06.split('?')[1]}》.png`;
            eleLink.style.display = 'none';
            eleLink.href = URL.createObjectURL(blob);
            document.body.appendChild(eleLink);
            eleLink.click();
            document.body.removeChild(eleLink);
          },
          'image/png',
          1
        );
        loading.close();
      });
    },
    export_data() {
      let that = this;
      let str = `排名,排名变动,达人,抖音号,粉丝总数,平均点赞,平均评论,平均转发\n`;
      for (let i = 0; i < that.table_data01.length; i++) {
        str += `${that.table_data01[i].rank},${that.table_data01[i].change},\"${that.table_data01[
          i
        ].nickName.replace(/"/g, '""')}\",${that.table_data01[i].uniqueId}\t,${that.format_num(
          that.table_data01[i].totalFans
        )},${that.format_num(that.table_data01[i].favoriteIncr)},${that.format_num(
          that.table_data01[i].commentIncr
        )},${that.format_num(that.table_data01[i].shareIncr)}\n`;
      }
      let blob = new Blob([str], { type: 'text/plain;charset=utf-8' }); //解决中文乱码问题
      blob = new Blob([String.fromCharCode(0xfeff), blob], { type: blob.type });
      let object_url = window.URL.createObjectURL(blob);
      let link = document.createElement('a');
      link.href = object_url;
      let str_name = `《蓝V认证榜-${that.value06.split('?')[1]}》.csv`;
      link.download = str_name;
      document.body.appendChild(link);
      link.click();
      document.body.removeChild(link);
    },
    loadMore: function () {
      if (this.page == 31) {
        return false;
      }
      this.get_data();
    },
    go_to_page01(id) {
      let router = this.$router.resolve({
        path: `/talent_main/tdetails/${id}/${0}`
      });
      window.open(router.href, '_blank');
    },
    backTop() {
      document.body.scrollTop = document.documentElement.scrollTop = 0;
    },
    initHeight() {
      var scrollTop =
        window.pageYOffset || document.documentElement.scrollTop || document.body.scrollTop;
      this.isFixed = scrollTop > 280 ? true : false;
    }
  }
};
</script>
<style scoped lang="less">
/* 搜索框 */
.imgBox {
  background: url('../../../assets/wang/talentCompare/search-nohover.png') no-repeat 10px 7px, #fff;
  background-size: 15px 15px;
}
.imgBox:hover {
  background: url('../../../assets/wang/talentCompare/search.png') no-repeat 10px 7px, #fff;
  background-size: 15px 15px;
}
.data_time {
  margin-top: -52px;
  margin-bottom: 32px;
  margin-right: 0;
  text-align: right;
  > span:first-child {
    font-size: 14px;
    color: #888888;
    // margin-right: 9px;
    letter-spacing: 0;
  }
  > span:last-child {
    font-size: 15px;
    letter-spacing: 0;
  }
}
.table {
  thead {
    tr {
      th:nth-child(2) {
        text-align: left;
        padding-left: 10px;
      }
    }
    img {
      width: 5px;
      height: 10px;

      vertical-align: initial;
    }
  }
  .tip_img {
    width: 16px;
    height: 16px;
    vertical-align: -3px;
  }
  tbody {
    tr {
      height: 80px;
    }
  }
  .td_08 {
    text-align: center;
    button {
      width: 88px;
      height: 32px;
      background-color: transparent;
      border-radius: 4px;
      border: 1px solid #fd7f2c;
      font-size: 14px;
      font-weight: normal;
      font-stretch: normal;
      line-height: 30px;
      letter-spacing: 0px;
      color: #fd7f2c;
      text-align: center;
    }
    button:hover {
      border-color: #ff924b;
      color: #ff924b;
    }
  }
  .td_01,
  .td_05,
  .td_06,
  .td_07,
  .td_03,
  .td_04 {
    font-size: 15px;
    font-family: DINAlternate-Bold;
  }
  .td_02 {
    width: 40%;
    > div {
      justify-content: flex-start !important;
      section {
        width: 210px;
        > div:first-child {
          display: flex;
          align-items: center;
          > span {
            width: auto;
            max-width: 100%;
            word-break: break-all;
            font-size: 14px;
            font-weight: 600;
            color: #222222;
            margin-bottom: 5px;
          }
          > span:hover {
            color: #ff924b;
          }
          .img01 {
            margin-right: 6px;
            margin-left: 0;
            margin-top: -5px;
          }
          img {
            margin-top: -3px;
            width: 14px;
            height: 14px;
            margin-left: 6px;
          }
        }
        > div:last-child {
          width: 100%;
          word-break: break-all;
          font-size: 14px;
          color: #888;
        }
      }
      > img {
        border-radius: 50%;
        width: 50px;
        height: 50px;
        object-fit: cover;
        margin-right: 12px;
        border: 1px solid #e5e5e5;
      }
    }
  }
  .td_01 {
    width: 173px;
    > div {
      font-family: DINAlternate-Bold;
      display: flex;
      align-items: center;
      > span:first-child {
        display: inline-block;
        width: 60px;
        text-align: right;
      }
      > section {
        width: 90px;
        .new_font {
          font-size: 16px;
          vertical-align: 3px;
          color: #fd7f2c;
          margin-left: 8px;
        }
        img {
          width: 8px;
          height: 12px;
          margin-top: -3px;
          margin-left: 8px;
          margin-right: 6px;
        }
        span {
          font-size: 16px;
          vertical-align: 1px;
        }
        .gray_img {
          width: 12px !important;
          height: 8px !important;
          margin-left: 6px !important;
          margin-right: 4px !important;
        }
      }
    }
  }
  .td {
    > div {
      height: 70px;
      display: flex;
      align-items: center;
      justify-content: center;
    }
  }
}
.type {
  margin-top: 14px;
  margin-bottom: 14px;
  > div {
    display: inline-block;
  }
  .input_group {
    display: inline-block;
    margin-left: 14px;
    position: relative;
    width: 308px;
    height: 32px;
    border-radius: 4px 4px 4px 4px;
    border: solid 1px #e5e5e5;
    box-sizing: border-box;
    input {
      width: 100%;
      font-size: 14px;
      vertical-align: -1px;
      height: 100%;
      box-sizing: border-box;
      padding-left: 11px;
      padding-right: 38px;
      border: none;
      color: #222;
    }
    input::-webkit-input-placeholder {
      color: #b5b5b5 !important;
    }
    > div {
      position: absolute;
      right: 0;
      cursor: pointer;
      top: 0px;
      text-align: center;
      width: 38px;
      height: 30px;
      border-radius: 0px 4px 4px 0px;
      display: flex;
      align-items: center;
      justify-content: center;
      img {
        width: 15px;
      }
    }
  }
  .btn_group {
    text-align: right;
    float: right;
    button {
      width: 62px;
      height: 32px;
      border-radius: 4px;
      border: solid 1px #e5e5e5;
      font-size: 14px;
      // line-height: 30px;
      box-sizing: border-box;
      color: #555;
      text-align: center;
    }
    button:nth-of-type(2) {
      color: #67ac5b;
      border: 1px solid #67ac5b;
    }

    > button:nth-child(1) {
      margin-right: 10px;
      span {
        display: inline-block;
        width: 14px;
        height: 14px;
        background: url('../../../assets/zhao/fenx.png') no-repeat;
        background-size: 100% 100%;
        margin-right: 4px;
        position: relative;
        top: 2px;
      }
    }
    > button:nth-child(2) {
      span {
        display: inline-block;
        width: 14px;
        height: 14px;
        background: url('../../../assets/zhao/daoc.png') no-repeat;
        background-size: 100% 100%;
        margin-right: 4px;
        position: relative;
        top: 2px;
      }
    }
    button:nth-of-type(1):hover {
      color: #777;
      span {
        background: url('../../../assets/zhao/fenx_h.png') no-repeat;
        background-size: 100% 100%;
      }
    }
    button:nth-of-type(2):hover {
      color: #80be76;
      border-color: #80be76;
      span {
        background: url('../../../assets/zhao/daoc_h.png') no-repeat;
        background-size: 100% 100%;
      }
    }
  }
}
.filter {
  display: flex;
  align-items: center;
  > p:first-child {
    font-size: 14px;
    color: #888888;
    margin-right: 50px;
    line-height: 32px;
    white-space: nowrap;
    color: #555;
  }
  #el_select {
    margin-right: 30px;
  }
}
#blue_v {
  min-height: 100vh;
  padding-bottom: 1px;
  // 升级会员
  .upgradeBox {
    min-height: 60vh;
    position: relative;
    .rankUpgrade {
      position: absolute;
      top: 50%;
      left: 50%;
      transform: translate(-50%, -50%);
    }
  }
  .fixedThead {
    display: none;
    position: fixed;
    top: 60px;
    left: 200px;

    border-radius: 4px;
    font-size: 14px;
    font-weight: 600;
    color: #555;
    background-color: #f5f9ff;
    height: 48px;
    line-height: 48px;
    overflow: hidden;
    z-index: 99;
    font-size: 0;
    li {
      box-sizing: border-box;
      display: inline-block;
      font-size: 14px;
    }
    img {
      width: 5px;
      height: 10px;
      margin-left: 4px;
      vertical-align: initial;
    }
    &.isShow {
      display: block;
    }
  }
  [tc] {
    text-align: center;
  }
  [tl] {
    text-align: left;
  }
  [tr] {
    text-align: right;
  }
}
</style>
